package com.nttuan.model.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.nttuan.util.Support;
import com.nttuan.model.DataBaseHelper;
import com.nttuan.model.dao.service.CategoryService;
import com.nttuan.model.entity.Category;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by NT Tuan on 10/20/2014.
 */
public class CategoryDao implements CategoryService{

    private DataBaseHelper db = null;
    private static CategoryDao categoryDao;

    public static CategoryDao getInstance(Context context){
        if (categoryDao == null)
            categoryDao = new CategoryDao(context);
        return  categoryDao;
    }
    public CategoryDao(Context context){
        try{
            db = new DataBaseHelper(context);
        }catch (Exception e){}
    }

    @Override
    public Category createOrUpDate(Category category) {
        db.openDataBase();
        int newId = isExit(category.getCategoryName());
        if(newId == 0){
            create(category);
            newId = newId();
        }else {
            category.setCategoryId(newId);
            update(category);
        }
        db.close();
        return category(newId);
    }
    private int newId(){
        db.openDataBase();
        String[] column ={"max("+Category.CATEGORY_ID+") as newId"};
        Cursor cursor = db.select(true,Category.TABLE_NAME,column,null,null,null);
        if(cursor != null)
            if (cursor.moveToNext()){
                db.close();
                return cursor.getInt(cursor.getColumnIndex("newId"));
            }
        db.close();
        return  1;
    }
    @Override
    public int isExit(String categoryName) {
        db.openDataBase();
        String selection = Category.CATEGORY_NAME +"=?";
        String[] where = {categoryName};
        Cursor cursor = db.select(true,Category.TABLE_NAME,null,selection,where,null);
        if(cursor != null)
            if (cursor.moveToNext()){
                db.close();
                return cursor.getInt(cursor.getColumnIndex(Category.CATEGORY_ID));
            }
        db.close();
        return  0;
    }

    @Override
    public List<Category> categorys(boolean isActive) {
        db.openDataBase();
        List<Category> result = new ArrayList<Category>();
        String selection = Category.IS_ACTIVE +"=?";
        String[] where = {isActive ? "1":"0"};
        Cursor cursor = db.select(true,Category.TABLE_NAME,null,selection,where,null);
        if(cursor != null)
            while (cursor.moveToNext())
                result.add(getItem(cursor));
        db.close();
        return  result;

    }

    @Override
    public Category category(String categoryName, boolean isActive) {
        db.openDataBase();
        String selection = Category.CATEGORY_NAME +"=? and " + Category.IS_ACTIVE +"=? ";
        String[] where = {categoryName,isActive ? "1":"0"};
        Cursor cursor = db.select(true,Category.TABLE_NAME, null, selection, where, null);
        if(cursor != null)
            if (cursor.moveToNext()){
                db.close();
                return getItem(cursor);
            }
        return  null;
    }

    @Override
    public Category category(int categoryId, boolean isActive) {
        db.openDataBase();
        String selection = Category.CATEGORY_ID +"=? and " + Category.IS_ACTIVE +"=? ";
        String[] where = {categoryId + "",isActive ? "1":"0"};
        Cursor cursor = db.select(true, Category.TABLE_NAME, null, selection, where, null);
        if(cursor != null)
            if (cursor.moveToNext()){
                db.close();
                return getItem(cursor);
            }
        return  null;
    }

    @Override
    public Category category(int categoryId) {
        db.openDataBase();
        String selection = Category.CATEGORY_ID +"=?";
        String[] where = {categoryId+""};
        Cursor cursor = db.select(true,Category.TABLE_NAME, null, selection, where, null);
        if(cursor != null)
            if (cursor.moveToNext()){
                db.close();
                return getItem(cursor);
            }
        return  null;
    }

    @Override
    public boolean create(Category category) {
        db.openDataBase();
        ContentValues values = new ContentValues();
     //   values.put(Category.CATEGORY_ID,category.getCategoryId());
        values.put(Category.CATEGORY_NAME,category.getCategoryName());
        values.put(Category.CATEGORY_ICON,category.getCategoryIcon());
        values.put(Category.NOTE,category.getNote());
        values.put(Category.IS_ACTIVE,category.isActive());
        boolean result = db.insert(Category.TABLE_NAME,values);
        db.close();
        return result;
    }

    @Override
    public boolean remove(int categoryId) {
        db.openDataBase();
        String whereClause = Category.CATEGORY_ID + "=?";
        String[] whereArgs = {categoryId+""};
        boolean result = db.delete(Category.TABLE_NAME,whereClause,whereArgs);
        db.close();
        return result;
    }

    @Override
    public boolean update(Category category) {
        db.openDataBase();
        ContentValues values = new ContentValues();
        values.put(Category.CATEGORY_NAME,category.getCategoryName());
        values.put(Category.CATEGORY_ICON,category.getCategoryIcon());
        values.put(Category.NOTE,category.getNote());
        values.put(Category.IS_ACTIVE,category.isActive());
        String whereClause =Category.CATEGORY_ID +" =?";
        String[] whereArgs = {category.getCategoryId()+""};
        boolean result = db.update(Category.TABLE_NAME,values,whereClause,whereArgs);
        db.close();
        return result;
    }

    @Override
    public boolean active(int categoryId, boolean isActive) {
        db.openDataBase();
        ContentValues values = new ContentValues();
        values.put(Category.IS_ACTIVE,isActive);
        String whereClause =Category.CATEGORY_ID +" =?";
        String[] whereArgs = {categoryId+""};
        boolean result = db.update(Category.TABLE_NAME,values,whereClause,whereArgs);
        db.close();
        return result;
    }

    private Category getItem(Cursor cursor){
        Category category= new Category();
        category.setCategoryId(cursor.getInt(cursor.getColumnIndex(Category.CATEGORY_ID)));
        category.setCategoryName(cursor.getString(cursor.getColumnIndex(Category.CATEGORY_NAME)));
        category.setCategoryIcon(cursor.getString(cursor.getColumnIndex(Category.CATEGORY_ICON)));
        category.setNote(cursor.getString(cursor.getColumnIndex(Category.NOTE)));
        category.setActive(Support.toActive(cursor.getString(cursor.getColumnIndex(Category.IS_ACTIVE))));
        return category;
    }
}
